package client.adminGUI;

import java.io.IOException;
import client.backend.Connection;
import client.GUI.GUIConstants;
import client.GUI.dashboard.AccountsPanel;
import client.adminGUI.GUIAdmin;
import client.backend.user.User;

import java.awt.event.*;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import java.awt.Color;
import java.awt.BorderLayout;
import javax.swing.table.DefaultTableModel;

public class UsersPanel extends JPanel implements ActionListener{
	private JTable table;
	private JScrollPane scroll;
	private static UsersPanel instance = null;
	private User[] users;
	private JButton viewButton;
	private JButton refresh;
	
	
	private final String columns[] = {"ID","Username","Name","Email", "Leverage", "Active"};
	
	public static final int WIDTH = 500;
	public static final int HEIGHT = 400;
	public static final int X = 6;
	public static final int Y = 6;
	
	public UsersPanel(){
		this.setBounds(X, Y, WIDTH, HEIGHT);
		table = new JTable(createTable(),columns);
		table.setBounds(0, 0, WIDTH, HEIGHT-50);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		scroll = new JScrollPane(table);
		this.setLayout(null);
		//scroll.setLayout(null);
		scroll.setBounds(0, 0, WIDTH, HEIGHT-50);
		this.add(scroll, BorderLayout.CENTER);
		this.setBackground(GUIAdmin.Background_Color);
		
		viewButton = new JButton("View User");
		//viewButton.setBounds(X+(WIDTH/2)-50, Y+HEIGHT+6, 100, 30);
		viewButton.setBounds(WIDTH/2-50, HEIGHT-36, 100, 30);
		this.add(viewButton);
		
		viewButton.addActionListener(this);

	}
	
	/**
	 * Create a table containing ACE users
	 * @return	Table array of users
	 */
	public String[][] createTable(){
		String list[][];
		try{
			users = User.getUsers();
			list = new String[users.length][6];
			for(int i=0;i<users.length;i++){
				list[i][0]=users[i].getID()+"";
				list[i][1]=users[i].getUserName();
				list[i][2]=users[i].getRealName();
				list[i][3]=users[i].getEMail();
				list[i][4]=users[i].getLeverage()+"";
				list[i][5]=(users[i].isActive())? "Y" : "N";
			}
		} catch(IOException e){
			list = new String[1][6];
			for(int i=0;i<list[1].length;i++){
				list[0][i]="";
			}
		}
		return list;
	}
	
	/**
	 * Action catcher
	 */
	public void actionPerformed(ActionEvent e) {
		GUIAdmin.getInstance().clearMessage();
    	if (e.getSource() == viewButton){
    		if(table.getSelectedRow()>=0){
    			GUIAdmin.getInstance().showUser(users[table.getSelectedRow()].getID());
    		} else GUIAdmin.getInstance().setMessage("You must first select a user");
    	}
    	if (e.getSource() == refresh) {
    		refreshTable();
    	}
    }
	
	/**
	 * Refresh the users table
	 */
	public void refreshTable() {
		if ((scroll != null) && (table != null)) {		
			DefaultTableModel model = (DefaultTableModel)table.getModel();
			try {
				model.setDataVector(createTable(), columns);
			} catch (Exception e){
				e.printStackTrace();
			}
			
			scroll.updateUI();
			this.updateUI();
		}
	}
	
	/**
	 * Getter for UsersPanel
	 * @return Only instance of UsersPanel
	 */
	public static UsersPanel getInstance() {
		if (instance == null) {
			instance = new UsersPanel();
		}
		return instance;
	}
}
